//
//  DetailViewController.m
//  ForestPack
//
//  Created by 骆锴 on 2018/7/23.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "PackingDesignViewController.h"
#import "SubmitDemandsViewController.h"
#import "PackingDesignViewCell.h"
#import "PackingIntroduceTableViewCell.h"
#import "SubmitViewCell.h"

@interface PackinDesignViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) UILabel *titletext;
@property (nonatomic, strong) UIWebView *webview;
@property (nonatomic, strong) UIButton *submit;
@property (nonatomic, strong) NSString *content;


@end

@implementation PackinDesignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    [self setNavBar];
    [self setHead];
    [self setWebview];
    [self setsubmitbutton];
    
    _isOrNotDirecIdType = IsOrNotDirecIdType_Yes;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setNavBar {
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 14, 44)];
    [backBtn setImage:[UIImage imageNamed:@"返回白色"] forState:UIControlStateNormal];
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
    [backBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    [self setTitle:@"解决方案"];
}

-(void)setHead{
    UIView *rline = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenX375(15), ScreenX375(3), ScreenX375(23))];
    rline.backgroundColor = ThemeColor;
    [self.view addSubview:rline];
    
    _titletext = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(18), ScreenX375(150), ScreenX375(18))];
    _titletext.textAlignment = NSTextAlignmentLeft;
    _titletext.text = @"森林包装设计";
    _titletext.font = Kfont(18.0);
    [self.view addSubview:_titletext];
    
    UILabel *fline = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(0), ScreenX375(53), ScreenX375(375), ScreenX375(1))];
    [fline setBackgroundColor:REXADECIMALCOLOR(@"#dcdcdc")];
    [self.view addSubview:fline];
}

-(void)setWebview{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(ScreenX375(0), ScreenX375(55), SCREEN_WIDTH, ScreenX375(475))];
    webView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:webView];
    webView.scrollView.showsHorizontalScrollIndicator = NO;
    webView.scrollView.showsVerticalScrollIndicator = NO;
    [webView loadHTMLString:_content baseURL:nil];
}

-(void)setsubmitbutton{
    _submit = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(530), ScreenX375(349), ScreenX375(44))];
    _submit.backgroundColor = ThemeColor;
    _submit.layer.cornerRadius = 22.0;
    _submit.tag = 100;
    [_submit setTitle:@"提需求" forState:UIControlStateNormal];
    [_submit addTarget:self action:@selector(turnToSubmit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_submit];
    
}

- (void)setTableView {
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, ScreenX375(54), SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 2;
    }else
        return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        PackingIntroduceTableViewCell *cell = [PackingIntroduceTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
        return cell;
    }else if (indexPath.section == 1){
         PackingDesignViewCell *cell = [PackingDesignViewCell cellWithTableView:tableView andIndexPath:indexPath];
        return cell;
    }else{
        SubmitViewCell *cell = [SubmitViewCell cellWithTableView:tableView andIndexPath:indexPath];
        return cell;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 86;
    }else if (indexPath.section == 1){
        return 205;
    }else{
        return 250;
    }
}

-(void)click{
    NSLog(@"clicked");
}

-(void)turnToSubmit{
    SubmitDemandsViewController *vc = [[SubmitDemandsViewController alloc] init];
    vc.design_id = self.product_id;
    vc.isOrNotDirecIdType = self.isOrNotDirecIdType;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)getData{
    NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"id":_product_id}];
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/solution_detail",REQUESTHEADER] andParameter:parameter success:^(id successResponse){
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSLog(@"%@",successResponse);
            self.titletext.text = successResponse[@"data"][@"name"];
            NSString *strUrl1 = [successResponse[@"data"][@"content"] stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
            NSString *strUrl2 = [strUrl1 stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
            NSString *strUrl3 = [strUrl2 stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
            NSString *htmls = [NSString stringWithFormat:@"<html> \n"
                               "<head> \n"
                               "<style type=\"text/css\"> \n"
                               "body {font-size:%dpx;}\n"// 字体大小，px是像素
                               "</style> \n"
                               "</head> \n"
                               "<body>"
                               "<script type='text/javascript'>"
                               "window.onload = function(){\n"
                               "var $img = document.getElementsByTagName('img');\n"
                               "for(var p in  $img){\n"
                               "$img[p].style.width = '100%%';\n"// 图片宽度
                               "$img[p].style.height ='auto'\n"// 高度自适应
                               "}\n"
                               "}"
                               "</script>%@"
                               "</body>"
                               "</html>",30, strUrl3];
            self->_content = htmls;
            [self setWebview];
        }
        else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}


//-(void)turnTosubmit{
//    SubmitDemandsViewController *vc = [[SubmitDemandsViewController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
//}

@end
